/************************************************************
 * Project Name		[Thinking_In_Java]
 * File Name     	[pkg_api_testing.Java_Math_BigInteger.java]
 * Creation Date 	[14-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_api_testing;

import java.math.BigInteger;

/**
 * java.math.BigInteger
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Java_Math_BigInteger {

	/**
	 * 普通整数计算阶乘
	 * 
	 * @param n
	 *            要计算阶乘的值
	 * @return 阶乘的计算结果
	 */
	public long getFactorial1(int n) {
		if (n < 0 || n > 17) {
			System.err.println("n的值必须在[0,17]内");
			return -1;
		} else if (n == 0) {
			return 1;
		} else {
			long result = 1L;
			for (; n > 0; n--) {
				result *= n;
			}
			return result;
		}
	}

	/**
	 * 使用BigInteger类计算阶乘
	 * 
	 * @param n
	 *            要计算阶乘的值
	 * @return 阶乘的计算结果
	 */
	public static BigInteger getFactorial2(int n) {
		if (n < 0) {
			System.err.println("n的值必须大于0");
			return new BigInteger("-1");
		} else if (n == 0) {
			return new BigInteger("0");
		}
		BigInteger result = new BigInteger("1");
		for (; n > 0; n--) {
			result = result
					.multiply(new BigInteger((new Integer(n)).toString()));
		}
		return result;
	}

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		Java_Math_BigInteger factorial = new Java_Math_BigInteger();
		System.out.println(factorial.getFactorial1(15));
		System.out.println(getFactorial2(15));
	}

}
